package com.example.administrator.yuejian_2.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.activity.ClubDetailActivity;
import com.example.administrator.yuejian_2.activity.TrainDetailActivity;
import com.example.administrator.yuejian_2.bean.ClubCollectBean;
import com.example.administrator.yuejian_2.bean.CollectBean;
import com.example.administrator.yuejian_2.bean.TrainCollectBean;
import com.example.administrator.yuejian_2.interservice.ClubService;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.viewholder.ClubCollectListViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/11/9.
 */

public class ClubCollectListAdapter extends RecyclerView.Adapter {

    private List<ClubCollectBean> clubcollect = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    private int uid;
    private int club_id,state;


    public ClubCollectListAdapter(Context context, List<ClubCollectBean> clubcollect){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.clubcollect.addAll(clubcollect);
        SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_clubcollect,parent,false);
        ClubCollectListViewHolder holder = new ClubCollectListViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //绑定数据
        final ClubCollectListViewHolder clubcollectholder = (ClubCollectListViewHolder) holder;
        ClubCollectBean clubcollectlist = clubcollect.get(position);
        clubcollectholder.clubname.setText(clubcollectlist.getClub_name());
        getState(uid,club_id);
        clubcollectholder.collect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击收藏按钮 改变收藏状态
                getState(uid,club_id);
                if (state == 1){
                    //getState(user_id,train_id);
                    deleteCollect(uid,club_id);
                    clubcollectholder.collect_button.setImageResource(R.drawable.ic_collect);
                    getState(uid,club_id);
                }

                else {
                    addCollect(uid,club_id);
                    clubcollectholder.collect_button.setImageResource(R.drawable.ic_collected);
                    getState(uid,club_id);
                }
            }
        });
        String club_picture = clubcollectlist.getClub_picture();
        club_id = clubcollectlist.getClub_id();
        if (!club_picture.equals(""))
            Picasso.with(context).
                    load("http://10.0.2.2/yuejian16/Public/clubphoto/"+club_picture)
                    .into(clubcollectholder.clubpic);

        final int club_id = clubcollectlist.getClub_id();
        clubcollectholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ClubDetailActivity.class);
                intent.putExtra("club_id",club_id);
                context.startActivity(intent);

            }
        });


    }
    private void getState(int u_id,int c_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<CollectBean> call = service.getstate(u_id,c_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    state = 1;

                }
                else {
                    state = 0;
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }

        });
    }


    private void addCollect(int u_id, int c_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<CollectBean> call = service.addcollect(u_id,c_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }


        });

    }
    private void deleteCollect(int u_id, int c_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<CollectBean> call = service.deletecollect(u_id,c_id);
        call.enqueue(new Callback<CollectBean>() {

            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.body().getInfo().equals("1")){
                    Toast.makeText(context, "操作成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }
        });

    }


    @Override
    public int getItemCount() {

        return clubcollect.size();
    }
}


